/*************************************************
 * 
 * Copyright by Bruno Eberhard
 * 
 * Created on 23.07.2005
 *
 *************************************************/
package fightplan.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import fightplan.core.Resources;

@SuppressWarnings("serial")
public class ControlList extends JList {
	private static final int WIDTH = 80;
	private CardPanel cardPanel;

	public ControlList(CardPanel cardPanel) {
		this.cardPanel = cardPanel;
		setBackground(Color.GRAY);
		setCellRenderer(new ControlCellRenderer());
		setModel(new DefaultListModel());
		getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				int index = getSelectedIndex() >= 0 ? getSelectedIndex() : 0;
				ControlListElement element = (ControlListElement) getModel().getElementAt(index);
				ControlList.this.cardPanel.showCard(element.component);
			}
		});
	}

	public void addElement(Component component, String displayName, String iconName) {
		ControlListElement element = new ControlListElement();
		element.component = component;
		element.displayName = displayName;
		element.icon = Resources.getIcon(iconName);
		((DefaultListModel) getModel()).addElement(element);
	}

	// overwritten

	public Dimension getPreferredSize() {
		Dimension d = super.getPreferredSize();
		return new Dimension(WIDTH, d.height);
	}

	public Dimension getMaximumSize() {
		Dimension d = super.getMaximumSize();
		return new Dimension(WIDTH, d.height);
	}

	public Dimension getMinimumSize() {
		Dimension d = super.getMinimumSize();
		return new Dimension(WIDTH, d.height);
	}

	//

	private class ControlCellRenderer extends JLabel implements ListCellRenderer {

		public ControlCellRenderer() {
			setBorder(BorderFactory.createEmptyBorder(8, 2, 4, 2));
			setForeground(Color.WHITE);
			setVerticalTextPosition(SwingConstants.BOTTOM);
			setHorizontalTextPosition(SwingConstants.CENTER);
			setVerticalAlignment(SwingConstants.CENTER);
			setHorizontalAlignment(SwingConstants.CENTER);
			setOpaque(true);
		}

		public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
				boolean cellHasFocus) {
			ControlListElement element = (ControlListElement) value;
			setBackground(isSelected ? Color.LIGHT_GRAY : Color.GRAY);
			setText(element.displayName);
			setIcon(element.icon);
			return this;
		}

	}

	private class ControlListElement {
		public String displayName;
		public Component component;
		public Icon icon;
	}

}